<?php
namespace app\sisome\controller;
use think\Request;
use think\Db;
class K extends Common{	//K计划后台
    public function index(){
    	// die('234');
        //return $this->fetch('hello',['name'=>'thinkphp']);
        //return view('hello',['name'=>'thinkphp']);
        // 方法    说明
        // fetch   渲染模板输出
        // display 渲染内容输出
        // assign  模板变量赋值
        // engine  初始化模板引擎
        return view();
    }
    public function lun(){    //轮次信息
        $lists=db('k_lun')->where([])->order('lun desc')->paginate(10);
        $sum_sxf = db('k_true_out')->where('status',1)->count();    //当前总提链手续费
        $sum_kgf = db('k_true_out')->where('status',1)->sum('gasused');    //当前总提链矿工费
        $this->assign('lists', $lists);
        $this->assign('sum_sxf', $sum_sxf);
        $this->assign('sum_kgf', $sum_kgf);
        return view();
    }
    public function user(){    //参与用户
        $lists=db('k_user')->where([])->order('uid desc')->paginate(10);
        $this->assign('lists', $lists);
        return view();
    }
    public function truein(){    //参与列表
        $lists=db('k_true_in')->where([])->order('id desc')->paginate(10);
        $this->assign('lists', $lists);
        return view();
    }
    public function tilian_list(){   //提链列表
        $param = request()->param();
        $where = [];
        if(isset($param['status'])){
            if($param['status']<>'all'){
                $where['status']=$param['status'];
            }
        }else{
            $where['status']=0;
        }

        $lists = db('k_true_out')->where($where)->order('id asc')->paginate(10);
        $sum['all'] = db('k_true_out')->where([])->sum('money');
        $sum['ok'] = db('k_true_out')->where('status',1)->sum('money');
        $sum['ing'] = db('k_true_out')->where('status',3)->sum('money');
        $max_nonce = $this->setting['k_nonce'];
        $this->assign('sum', $sum);
        $this->assign('max_nonce', $max_nonce);
        $this->assign('lists', $lists);
        $this->assign('status', isset($param['status'])?$param['status']:'0');
        return view();
    }
    public function tilian_do(){   //提链执行
        $setting = $this->setting;
        $param = request()->param();
        $where = [];
        //合法性
        if(!isset($param['id'])){
            $this->error('非法请求！');
        }
        $item = db('k_true_out')->where('id',$param['id'])->find();
        if(!$item){
            $this->error('不存在！');
        }elseif(!in_array($item['status'],[0,2])){
            $this->error('请勿重复处理！'.$item['status']);
        }
        //接口执行转账
        $user_qb=db('adeth')->where('uname',$item['uname'])->find();
        $urlqb=config('coin_host2').'sendTransaction';
        $params['type']='gusd';
        $params['contract']=config('con_gusd');
        $params['from']=$setting['k_address'];
        $params['nonce']=$setting['k_nonce'];
        $params['key']=$setting['k_key'];
        $params['to']=$user_qb['address'];
        $params['value']=floatval($item['money']);  //去除小数点后多余0
        // $params['gas']=10;
        // die($urlqb.'?'.http_build_query($params));

        $retqb=http($urlqb,$params,'GET',30);
        if(!$retqb){
            $this->error('接口失败');
        }
        $retqb=json_decode($retqb,true);
        if($retqb && $retqb['status']==1 && $retqb['hash']){
            $ret1 = db('k_true_out')->where('id',$item['id'])->update([
                'status'=>3,
                'uptime'=>time(),
                'hash'=>$retqb['hash'],
                'nonce'=>$setting['k_nonce'],
            ]);
            $ret2 = db('setting')->where('key','k_nonce')->setInc('val');
            if($ret1 && $ret2){
                $this->success('执行成功');
            }else{
                $this->error('提链成功，但写入失败!!');
            }
        }else{
            db('k_true_out')->where('id',$item['id'])->update([
                'status'=>2,
                'uptime'=>time(),
            ]);
            $this->error('提链失败');
        }
    }
    public function tilian_status(){    //提链结果手动查询
        $param = request()->param();
        $where = [];
        //合法性
        if(!isset($param['id'])){
            $this->error('非法请求！');
        }
        $item = db('k_true_out')->where('id',$param['id'])->find();
        if(!$item){
            $this->error('不存在！');
        }elseif($item['status']<>3){
            $this->error('请勿重复查询！');
        }

        $urlqb=config('coin_host2').'getTransactionHash';
        $params['hash']=$item['hash'];
        $ret_qb=http($urlqb,$params,'GET');
        $ret_qb=json_decode($ret_qb,true);
        // dump($ret_qb);
        // dump($ret_qb['hash']['status']);
        // dump($ret_qb['hash']['gasUsed']);
        // dump($ret_qb['hash']['logs'][0]['blockNumber']);
        // die();
        if($ret_qb && isset($ret_qb['hash']['status']) && $ret_qb['hash']['status']){
            db('k_true_out')->where('id',$item['id'])->update([
                'status'=>1,
                'blocknumber'=>$ret_qb['hash']['logs'][0]['blockNumber'],
                'gasused'=>$ret_qb['hash']['gasUsed']
            ]);
            $this->success('已到账');
        }else{
            $this->success('还未到账');
        }
    }

    public function tilian_nonce(){ //修正nonce
        $param = request()->param();
        if (!isset($param['nonce'])) {
            $this->error('请输入修正内容');
        }
        $param['nonce'] = intval($param['nonce']);
        if($param['nonce']<=0){
            $this->error('请输入正数');
        }
        $ret = db('setting')->where('key','k_nonce')->update(['val'=>$param['nonce']]);
        if($ret){
            $this->success('修改成功，请调试提链');
        }else{
            $this->error('修改失败');
        }
    }
    public function dong(){    //动态记录
        $lists=db('k_log_dong')->where([])->order('id desc')->paginate(10);
        $this->assign('lists', $lists);
        return view();
    }
}
